package com.xj.api.bussiness.reference.web;

import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.DatePattern;
import com.feilong.core.Validator;
import com.feilong.core.date.DateUtil;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.deviceInformation.service.ITbDeviceInformationService;
import com.xj.api.bussiness.deviceInformationImg.service.ITbDeviceInformationImgService;
import com.xj.api.bussiness.machin.service.YzMachinInfoService;
import com.xj.api.bussiness.personalInformationImg.service.ITbPersonalInformationImgService;
import com.xj.api.bussiness.reference.service.YzUserMachinReferenceService;
import com.xj.api.bussiness.registerLicense.service.ITbRegisterLicenseService;
import com.xj.api.bussiness.transferLicense.service.ITbTransferLicenseService;
import com.xj.api.bussiness.userInfo.service.ITbUserInfoService;
import com.xj.api.bussiness.yzuser.service.YzUserService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.util.ObjectUtils;
import com.xj.common.bussiness.deviceInformation.entity.TbDeviceInformation;
import com.xj.common.bussiness.deviceInformationImg.entity.TbDeviceInformationImg;
import com.xj.common.bussiness.machin.entity.YzMachinInfo;
import com.xj.common.bussiness.personalInformationImg.entity.TbPersonalInformationImg;
import com.xj.common.bussiness.reference.entity.YzUserMachinReference;
import com.xj.common.bussiness.registerLicense.entity.TbRegisterLicense;
import com.xj.common.bussiness.transferLicense.entity.TbTransferLicense;
import com.xj.common.bussiness.userInfo.entity.TbUserInfo;
import com.xj.common.bussiness.yzuser.entity.YzUser;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Mars
 * @since 2017-08-24
 */
@RestController
@Slf4j
@RequestMapping("yzUserMachinReference")
public class YzUserMachinReferenceController extends BaseController{
	
	@Autowired
	private YzUserMachinReferenceService yzUserMachinReferenceService;
	@Autowired
	private YzMachinInfoService yzMachinInfoService;
	@Autowired
	private YzUserService yzUserService;
	@Autowired
	private ITbUserInfoService iTbUserInfoService;
	@Autowired
	private ITbPersonalInformationImgService iTbPersonalInformationImgService;
	@Autowired
	private ITbDeviceInformationService iTbDeviceInformationService;
	@Autowired
	private ITbDeviceInformationImgService iTbDeviceInformationImgService;
	@Autowired
	private ITbRegisterLicenseService iTbRegisterLicenseService;
	@Autowired
	private ITbTransferLicenseService iTbTransferLicenseService;
	@GetMapping("/list.auth")
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean yzUserMachinReferenceList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(yzUserMachinReferenceService.selectMapsPage(
				new Page<YzUserMachinReference>(pageNum, pageSize),
				new EntityWrapper<YzUserMachinReference>()));
	}
	
	@GetMapping("/{phone}.auth")
	@ApiOperation(value = "获取用户设备备案詳情", httpMethod = "GET", notes = "获取用户设备备案詳情")
	public AbstractBean yzuserContent(@PathVariable Integer id){
		return json(yzUserMachinReferenceService.selectOne(new EntityWrapper<YzUserMachinReference>().eq("id", id)));
	}
	
	@GetMapping("/findReferenceByIdCardNumList")
	@ApiOperation(value = "获取用户设备备案詳情", httpMethod = "GET", notes = "获取用户设备备案詳情")
	public AbstractBean findReferenceByIdCardNumList(@RequestParam String idCardNum){
		return json(yzUserMachinReferenceService.findReferenceByIdCardNum(idCardNum));
	}
	
	@GetMapping("/findReferenceByIdCardNumV2")
	@ApiOperation(value = "获取用户设备备案詳情改版", httpMethod = "GET", notes = "获取用户设备备案改版")
	public AbstractBean findReferenceByIdCardNumV2(@RequestParam String idCardNum){
		return json(yzUserMachinReferenceService.findReferenceByIdCardNumV2(idCardNum));
	}
	
	
	@PostMapping("/save")
	@ApiOperation(value = "新增", httpMethod = "POST", notes = "新增")
	public AbstractBean add(YzUserMachinReference yzUserMachinReference){		
		if(!yzUserMachinReferenceService.insert(yzUserMachinReference)){
		return error();
	    }
		return success();
	}
	
	@PostMapping("/update")
	@ApiOperation(value = "修改", httpMethod = "POST", notes = "新增")
	public AbstractBean update(YzUserMachinReference yzUserMachinReference){		
		if(!yzUserMachinReferenceService.updateById(yzUserMachinReference)){
		return error();
	    }
		return success();
	}
	
	

	
	
	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!yzUserMachinReferenceService.deleteById(id)){
		return error();
		}
		return success();
    }
	
	
	@PostMapping("/saveUserMachinReference")
	@ApiOperation(value = "新增用户设备备案信息", httpMethod = "POST", notes = "新增")
	public AbstractBean addUserMachinReference(@RequestBody Map<String,Object>map) throws Exception{
		String factoryDate = (String) map.get("factoryDate");
		if(Validator.isNotNullOrEmpty(factoryDate)){
			map.put("factoryDate", DateUtil.toDate(factoryDate, DatePattern.COMMON_DATE));
		}

		String purchaseDate = (String) map.get("purchaseDate");
		if(Validator.isNotNullOrEmpty(factoryDate)){
			map.put("purchaseDate", DateUtil.toDate(purchaseDate, DatePattern.COMMON_DATE));
		}

		YzUser yzUser=(YzUser) ObjectUtils.transforMapToObject(YzUser.class,map);
		yzUserService.insert(yzUser);
		YzMachinInfo yzMachinInfo=(YzMachinInfo) ObjectUtils.transforMapToObject(YzMachinInfo.class,map);
		yzMachinInfo.setUserId(yzUser.getId());
		yzMachinInfoService.insert(yzMachinInfo);
		YzUserMachinReference yzUserMachinReference=new YzUserMachinReference();
		yzUserMachinReference.setUserId(yzUser.getId());
		yzUserMachinReference.setMachineId(yzMachinInfo.getId());
		yzUserMachinReference.setAuditStatus("0");//未审核
		yzMachinInfo.setUserId(yzUser.getId());
		yzUserMachinReferenceService.insert(yzUserMachinReference);
		return success();
	}
	
	
	@PostMapping("/saveUserMachinReferenceV2")
	@ApiOperation(value = "新增用户设备备案信息", httpMethod = "POST", notes = "新增")
	public AbstractBean saveUserMachinReferenceV2(@RequestBody Map<String,Object>map) throws Exception{
		addUserMachinReferenceV2(map);
		return success();
	}
	
	@Transactional(readOnly=false)
	public void  addUserMachinReferenceV2(Map<String,Object>map) throws Exception{

		log.info("前端获取的信息：{}",map.toString());
		String dateOfProduction = (String) map.get("dateOfProduction");
		if(Validator.isNotNullOrEmpty(dateOfProduction)){
			map.put("dateOfProduction", DateUtil.toDate(dateOfProduction, DatePattern.COMMON_DATE));
		}

		String resBuyDate = (String) map.get("resBuyDate");
		if(Validator.isNotNullOrEmpty(resBuyDate)){
			map.put("resBuyDate", DateUtil.toDate(resBuyDate, DatePattern.COMMON_DATE));
		}
		//新增设备备案信息
		TbDeviceInformation deviceInformation=(TbDeviceInformation)
				ObjectUtils.transforMapToObject(TbDeviceInformation.class,map);
		deviceInformation.setCreateDate(new Date());		
		
		//新增用户备案信息
		TbUserInfo tbUserInfo=(TbUserInfo) ObjectUtils.transforMapToObject(TbUserInfo.class,map);
		tbUserInfo.setCreateDate(new Date());
		tbUserInfo.setIdentification(deviceInformation.getIdentification());
		deviceInformation.setDeviceRegisterName(tbUserInfo.getRegisterName());
		iTbUserInfoService.insert(tbUserInfo);

		iTbDeviceInformationService.insert(deviceInformation);
		//新增用户备案图片信息
		TbPersonalInformationImg tbPersonalInformationImg=(TbPersonalInformationImg)
				ObjectUtils.transforMapToObject(TbPersonalInformationImg.class,map);
		tbPersonalInformationImg.setUserInfoId(tbUserInfo.getId());
		tbPersonalInformationImg.setCreateDate(new Date());
		tbPersonalInformationImg.setIdentityNum(deviceInformation.getIdentification());
		tbPersonalInformationImg.setBigShelfNum(deviceInformation.getFactoryNum());
		iTbPersonalInformationImgService.insert(tbPersonalInformationImg);		
		
		//新增设备备案图片信息
		TbDeviceInformationImg tbDeviceInformationImg =(TbDeviceInformationImg)
				ObjectUtils.transforMapToObject(TbDeviceInformationImg.class,map);
		tbDeviceInformationImg.setDeviceInformationId(deviceInformation.getId());
		tbDeviceInformationImg.setCreateDate(new Date());
		tbDeviceInformationImg.setBigShelfNum(deviceInformation.getFactoryNum());
		tbDeviceInformationImg.setIdentityNum(deviceInformation.getIdentification());
		iTbDeviceInformationImgService.insert(tbDeviceInformationImg);
		
		YzUserMachinReference yzUserMachinReference=new YzUserMachinReference();
		yzUserMachinReference.setUserId(tbUserInfo.getId());
		yzUserMachinReference.setMachineId(deviceInformation.getId());
		yzUserMachinReference.setAuditStatus("0");//未审核
		yzUserMachinReferenceService.insert(yzUserMachinReference);
		
		//插入上户记录信息
		TbRegisterLicense registerLicense=new TbRegisterLicense();
		registerLicense.setFactoryNum(deviceInformation.getFactoryNum());
		registerLicense.setIdentification(deviceInformation.getIdentification());
		registerLicense.setCreateDate(new Date());
		iTbRegisterLicenseService.insert(registerLicense);

		//插入过户记录信息
		TbTransferLicense tbTransferLicense=new TbTransferLicense();
		tbTransferLicense.setIdentification(deviceInformation.getIdentification());
		tbTransferLicense.setFactoryNum(deviceInformation.getFactoryNum());
		tbTransferLicense.setCreateDate(new Date());
		iTbTransferLicenseService.insert(tbTransferLicense);
	}
}
